/**
 * 
 */
package com.ruyipu.forum.adapter;

import java.util.List;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
import android.widget.TextView;

import com.ruyipu.forum.R;
import com.ruyipu.forum.interfaces.IItem;
import com.ruyipu.forum.item.Category;

/**
 * @author donald
 * 
 */
public class CategoryAdapter extends BaseAdapter {

	/**
	 * 
	 */
	public CategoryAdapter(Context context, List<IItem> dataList) {
		super(context, dataList);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see android.widget.Adapter#getView(int, android.view.View,
	 * android.view.ViewGroup)
	 */
	public View getView(int position, View convertView, ViewGroup parent) {
		LinearLayout layout = (LinearLayout) getView(convertView, R.layout.category_item);
		View vLeft = (View) layout.findViewById(R.id.vLeft);
		TextView tvCategoryName = (TextView) layout
				.findViewById(R.id.tvCategoryName);
		TextView tvCategoryDesc = (TextView) layout
				.findViewById(R.id.tvCategoryDescription);
		TextView tvCommentCount = (TextView) layout
				.findViewById(R.id.tvCategoryCommentCount);
		TextView tvDiscussionCount = (TextView) layout
				.findViewById(R.id.tvCategoryDiscussionCount);
		Category item = (Category) dataList.get(position);
		int iHeight = LinearLayout.LayoutParams.FILL_PARENT;
		int iWidth = 20;
		if (item.getParentCategoryId() == -1) {
			vLeft.setLayoutParams(new LayoutParams(iWidth * 0, iHeight));
		} else {
			vLeft.setLayoutParams(new LayoutParams(iWidth * 1, iHeight));
		}
		tvCategoryName.setText(item.getName());
		if (item.getDesc().trim().equals("")) {
			tvCategoryDesc.setLayoutParams(new LayoutParams(
					LinearLayout.LayoutParams.FILL_PARENT, 0));
		} else {
			tvCategoryDesc.setText(item.getDesc());
		}

		tvCommentCount.setText(String.format(
				context.getString(R.string.comment_count),
				item.getCommentCount()));
		tvDiscussionCount.setText(String.format(
				context.getString(R.string.discussion_count),
				item.getDiscussionCount()));
		return layout;
	}
}
